<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/26
 * Time: 21:00
 */
namespace app\admin\service;
use app\models\AuthModel;

/**
 *
 * User: wufeng
 * Date: 2019/4/1
 * @package app\admin\service
 */
class AuthService
{
    /**
     * @return array
     */
    public function auth()
    {
        try {
            $page = input('page',1);
            $listRows = input('limit',5);
            $title = input('title');
            $status = input('status');
            $type_status = input('type_status');
            $where = [];
            if($title) $where['title'] = $title;

            if($status != ''){
                $where['status'] = $status;
            }else{
                $where['status'] = ['neq',-1];
            }



            $order = 'id asc';
			if (get_current_type() == 1){//超级管理员

			}elseif (get_current_type() == 2){//系统管理员
                $where['type'] = ['in','3,4,5'];
				$where['id'] = ['neq',get_current_group_id()];
				$where['company_id'] = get_current_company_id();
			}elseif (get_current_type() == 3){//区域管理员
                $where['type'] = ['in','4,5'];
				$where['id'] = ['neq',get_current_group_id()];
				$where['company_id'] = get_current_company_id();
			}elseif (get_current_type() == 4){//校区管理员
                $where['type'] = ['in','5'];
				$where['id'] = ['neq',get_current_group_id()];
				$where['company_id'] = get_current_company_id();
			}

            if($type_status != ''){
                $where['type'] = $type_status;
            }

            return AuthModel::pageLists($where, $page,$listRows, $order);
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

	/**
	 * 添加用户组
	 * User: wufeng
	 * Date: 2019/4/1
	 *
	 * @return array
	 * @throws \think\exception\DbException
	 */
    public function auth_add()
    {
        $data = input();
        $groupModel = new AuthModel();
		if(get_current_type() != 1){
			$data['company_id'] = get_current_company_id();
		}
        /*校验：是否重复提交*/
        $check = $groupModel::get(['company_id' => $data['company_id'],'title' => $data['title']]);
        if ($check) {
            return ['code'=>1,'msg'=>"用户组已存在"];
        }
        $group = $groupModel::create($data)->save();
        if($group !== false){
            return ['code'=>0,'msg'=>"添加成功"];
        }
        return ['code'=>1,'msg'=>"添加失败"];
    }

	/**
	 * 修改用户组
	 * User: wufeng
	 * Date: 2019/4/1
	 *
	 * @return array
	 * @throws \think\exception\DbException
	 */
    public function auth_edit()
    {
        $data = input('post.');
        $groupModel = new AuthModel();
        /*校验：是否重复提交*/
        $check = $groupModel::get(['company_id' => $data['company_id'],'title'=>$data['title']]);
        if ($check) {
            if($check['id'] != $data['id']){
                return ['code'=>1,'msg'=>"用户组已存在"];
            }
        }
        $group = $groupModel::update($data);
        if($group !== false){
            return ['code'=>0,'msg'=>"修改成功"];
        }
        return ['code'=>1,'msg'=>"修改失败"];
    }

	/**
	 * 分配权限
	 * User: wufeng
	 * Date: 2019/4/1
	 *
	 * @return array
	 */
    public function rule_group()
    {
        $data = input('post.');
        if(isset($data['menu']) && !empty($data['menu'])){
			$rule_ids = implode(",", $data['menu']);
		}else{
			return ['code' => 1, 'msg' => "请选择需要分配的权限"];
		}
        $role_id = $data['id'];
        $groupModel = new AuthModel();
        if($groupModel->addAuthRule($rule_ids, $role_id) !== false){
            return ['code' => 0, 'msg' => "分配成功"];
        }
        return ['code' => 1, 'msg' => "分配失败"];
    }
}